package com.quectel.core.module.notice.service.impl;


import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.notice.dao.NoticeActivityOutlinkDao;
import com.quectel.core.module.notice.dto.NoticeActivityOutlinkDto;
import com.quectel.core.module.notice.entity.NoticeActivityOutlinkEntity;
import com.quectel.core.module.notice.service.NoticeActivityOutlinkService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-12-20 11:35:24
 */
@DubboService
public class NoticeActivityOutlinkServiceImpl implements NoticeActivityOutlinkService {

    private static final Logger LOGGER = LoggerFactory.getLogger(NoticeActivityOutlinkServiceImpl.class);

    private static final String NOTICE_ACTIVITY_OUTLINK_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "NOTICE_ACTIVITY_OUTLINK_CACHE_BY_ID:";

    @Autowired
    private NoticeActivityOutlinkDao noticeActivityOutlinkDao;


    @Override
    public NoticeActivityOutlinkDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                NOTICE_ACTIVITY_OUTLINK_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public NoticeActivityOutlinkDto selectById(Long id) {
        NoticeActivityOutlinkEntity entity = noticeActivityOutlinkDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, NoticeActivityOutlinkDto.class));
    }

    @Override
    public List<NoticeActivityOutlinkDto> queryList(Map<String, Object> params) {
        List<NoticeActivityOutlinkEntity> list = noticeActivityOutlinkDao.queryList(params);
        List<NoticeActivityOutlinkDto> result = CopyUtils.copyList(list, NoticeActivityOutlinkDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private NoticeActivityOutlinkDto paddingField(NoticeActivityOutlinkDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return noticeActivityOutlinkDao.queryTotal(params);
    }

    @Override
    public Long save(NoticeActivityOutlinkDto dto) {
        NoticeActivityOutlinkEntity entity = CopyUtils.copyObj(dto, NoticeActivityOutlinkEntity.class);
        entity.setId(Snowflake.nextId());
        noticeActivityOutlinkDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(NoticeActivityOutlinkDto dto) {
        NoticeActivityOutlinkEntity entity = CopyUtils.copyObj(dto, NoticeActivityOutlinkEntity.class);
        noticeActivityOutlinkDao.updateById(entity);
        CacheKit.invalidRedisCache(NOTICE_ACTIVITY_OUTLINK_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(NoticeActivityOutlinkDto dto) {
        NoticeActivityOutlinkEntity entity = CopyUtils.copyObj(dto, NoticeActivityOutlinkEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, noticeActivityOutlinkDao);

        CacheKit.invalidRedisCache(NOTICE_ACTIVITY_OUTLINK_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        noticeActivityOutlinkDao.deleteById(id);

        CacheKit.invalidRedisCache(NOTICE_ACTIVITY_OUTLINK_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
